﻿using LightCAD.Core;
using LightCAD.Core.Elements;
using SkiaSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace LightCAD.Runtime.Editor
{
    public static class CircleDrawer
    {
        public static void DrawTo(this Circle circle, SKCanvas canvas, Vector2d offset)
        {
            //canvas.DrawPoint(new SKPoint(1, 1), ElementDrawer.defaultPen);
            //return;
            var wc = ElementDrawer.PointWorldToScr(circle.Center+offset);
            var wr = ElementDrawer.LengthWorldToScr(circle.Radius);


            //SKPathEffect effect = null;
            //if (circle.IsSelected)
            //{
            //    effect = SKPathEffect.CreateDash(new float[] { 3, 3 }, 10);
            //}
            //using (var pen = new SKPaint { Color = SKColors.White, IsStroke = true,PathEffect= effect })
            //{
            //    canvas.DrawCircle(wc, wr, pen);
            //}
            bool isDragCopy = (offset != Vector2d.Zero);
            var pen = ElementDrawer.GetDrawPen(circle, isDragCopy);
            canvas.DrawCircle(wc, wr, pen);
        }
        public static ControlGrip[] GetControlGrips(this Circle circle)
        {
            var grips = new List<ControlGrip>();
            var gripCenter = new ControlGrip
            {
                Element = circle,
                Name = "Center",
                Position = circle.Center
            };
            grips.Add(gripCenter);

            var gripLeft = new ControlGrip
            {
                Element = circle,
                Name = "Left",
                Position = new Vector2d(circle.Center.X - circle.Radius, circle.Center.Y)
            };
            grips.Add(gripLeft);

            var gripTop = new ControlGrip
            {
                Element = circle,
                Name = "Top",
                Position = new Vector2d(circle.Center.X , circle.Center.Y + circle.Radius)
            };
            grips.Add(gripTop);

            var gripRight = new ControlGrip
            {
                Element = circle,
                Name = "Right",
                Position = new Vector2d(circle.Center.X+circle.Radius, circle.Center.Y )
            };
            grips.Add(gripRight);

            var gripBottom = new ControlGrip
            {
                Element = circle,
                Name = "Bottom",
                Position = new Vector2d(circle.Center.X, circle.Center.Y - circle.Radius)
            };
            grips.Add(gripBottom);
            return grips.ToArray();
        }
        private static string _gripName;
        private static Vector2d _position;
        private static Circle _circle;
        public static Action<SKCanvas> _SetDragGrip(this Circle circle, string gripName, Vector2d position,bool isEnd)
        {
            _circle= circle; 
            if (!isEnd)
            {
                _gripName = gripName;
                _position = position;
                return DrawDragGrip;
            }
            else
            {
                if (gripName == "Center")
                    circle.Set(center: position);
                else
                {
                    var r = Vector2d.Distance(circle.Center, position);
                    circle.Set(radius: r);
                }
                return null;
            }
        }
        public static void DrawDragGrip(SKCanvas canvas)
        {
            var center = ElementDrawer.PointWorldToScr(_circle.Center);
            var r = ElementDrawer.LengthWorldToScr(_circle.Radius);
            if (_gripName == "Center")
                center = ElementDrawer.PointWorldToScr(_position);
            else
            {
                var wr = Vector2d.Distance(_circle.Center, _position);
                r= ElementDrawer.LengthWorldToScr(wr);
            }
            canvas.DrawCircle(center, r, ElementDrawer.draggingPen);
        }
    }

}
